package com.autoglm.di

import android.content.Context
import androidx.room.Room
import com.autoglm.data.local.AutoGLMDatabase
import com.autoglm.data.local.dao.TaskDao
import com.autoglm.data.local.dao.UserPreferenceDao
import com.autoglm.data.remote.GLMApiService
import com.autoglm.data.repository.GLMRepository
import com.autoglm.data.repository.TaskRepository
import com.autoglm.data.repository.UserPreferenceRepository
import com.autoglm.service.AutoGLMAccessibilityService
import com.autoglm.service.ScreenCaptureService
import com.autoglm.service.TaskExecutionService
import com.autoglm.utils.Constants
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Singleton

/**
 * 应用程序依赖注入模块
 * 提供全局单例对象
 */
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    
    @Provides
    @Singleton
    fun provideOkHttpClient(): OkHttpClient {
        val loggingInterceptor = HttpLoggingInterceptor().apply {
            level = HttpLoggingInterceptor.Level.BODY
        }
        
        return OkHttpClient.Builder()
            .addInterceptor(loggingInterceptor)
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
            .build()
    }
    
    @Provides
    @Singleton
    fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl(Constants.GLM_API_BASE_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }
    
    @Provides
    @Singleton
    fun provideGLMApiService(retrofit: Retrofit): GLMApiService {
        return retrofit.create(GLMApiService::class.java)
    }
    
    @Provides
    @Singleton
    fun provideAutoGLMDatabase(@ApplicationContext context: Context): AutoGLMDatabase {
        return Room.databaseBuilder(
            context,
            AutoGLMDatabase::class.java,
            "autoglm_database"
        ).build()
    }
    
    @Provides
    fun provideTaskDao(database: AutoGLMDatabase): TaskDao {
        return database.taskDao()
    }
    
    @Provides
    fun provideUserPreferenceDao(database: AutoGLMDatabase): UserPreferenceDao {
        return database.userPreferenceDao()
    }
    
    @Provides
    @Singleton
    fun provideGLMRepository(apiService: GLMApiService): GLMRepository {
        return GLMRepository(apiService)
    }
    
    @Provides
    @Singleton
    fun provideTaskRepository(taskDao: TaskDao): TaskRepository {
        return TaskRepository(taskDao)
    }
    
    @Provides
    @Singleton
    fun provideUserPreferenceRepository(userPreferenceDao: UserPreferenceDao): UserPreferenceRepository {
        return UserPreferenceRepository(userPreferenceDao)
    }
}
